$(document).ready(function(){
    var username = $('#username'),
    email = $('#email'),
    password = $('#password'),
    password2 = $('#password2'),
    userHelp = $('#username-help'),
    pwdHelp = $('#password-help'),
    emailHelp = $('#email-help'),
    pwd2Help = $('#password2-help');
    
    var s1 = false,s2 = false,s3 = false,s4 = false;
    
    username.focus(function(){
        userHelp.text('');
    }).blur(function(){
        s1 = true;
        var result = dynaValidate('username',username.val(),/^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]{5,15}$/,userHelp);
        if(result == 0){
            s1 = false;
            userHelp.text("请输入正确的用户名(只含有汉字,数字,字母,下划线不能以下划线开头和结尾,长度为6到15)");
        }
    });
    
    email.focus(function(){
        emailHelp.text('');
    }).blur(function(){
        s2 = true;
        var result = dynaValidate('email',email.val(),/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,emailHelp);
        if(result == 0){
            s2 = false;
            emailHelp.text("请输入正确的邮箱(example@xxx.com)");
        }
    });
    
    password.focus(function(){
        pwdHelp.text('');
    }).blur(function(){
        s3 = true;
        var result = statValidate(password.val(),/^[a-zA-Z]\w{5,20}$/);
        if(result == 0){
            s3 = false;
            pwdHelp.text("请输入正确的密码(以字母开头，长度在6~20之间，只能包含字符,数字和下划线)");
        }
    });
   
    password2.focus(function(){
        pwd2Help.text('');
    }).blur(function(){
        s4 = true;
        var value = $.trim(password2.val());
        var pwd = $.trim(password.val());
        if(value !== pwd){
            s4 = false;
            pwd2Help.text('请输入正确的密码(两次密码不一样)');
        }
    });
    
    $('#submitBtn').click(function(){
        username.trigger('blur');
        email.trigger('blur');
        password.trigger('blur');
        password2.trigger('blur');
        
        if(s1 && s2 && s3 && s4){
            if(username.val() != '' && nick.val() != '' && email.val() != '' && password.val() != '' 
                && profile.val() != '' && password2.val() != '' && userHelp.text() == '' &&
            emailHelp.text() == '' && pwdHelp.text() == '' && pwd2Help.text() == ''){
                $(this).submit();
            }else{
                $('#submitBtn-help').text("请正确填写信息");
                return false;
            }
        }
        return false;
    });
})


function dynaValidate(col,value,exp,help){
    var result = 0;
    if(exp.test(value)){
        result = 1;
        $.ajax({
            type : 'POST',
            dataType : 'json',
            url : '/site/validate',
            data : {
                'col':col,
                'colVal':value
            },
            success : function(data){
                if(data.r == 0){
                    help.text(data.msg);
                }
            },
            fail : function(data){
                result = -1;//请求错误
            }
        });
    }else{
        result = 0;
    }
    return result;
}

function statValidate(value, exp){
    var result = 0;
    if(exp.test(value)){
        result = 2;
    }
    return result;
}
